package com.lucenten.collect.gnss.handler.impl;

import cn.quevo.common.lang.NumberUtils;
import cn.quevo.common.lang.StringUtils;
import com.lucenten.collect.exec.ShellRun;
import com.lucenten.collect.gnss.entity.GnssFile;
import com.lucenten.collect.gnss.handler.AbstractGnssHandler;
import com.lucenten.collect.gnss.handler.GnssHandler;
import com.lucenten.collect.gnss.handler.tools.GnssContext;
import com.lucenten.collect.gnss.service.ObsErvationService;
import com.lucenten.collect.gnss.service.SatelliteStationService;
import com.lucenten.collect.utils.FileUtils;
import com.lucenten.mq.SendJmsInfo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 第二步：.dat格式转换
 */
public class TransGnssHandler extends AbstractGnssHandler implements GnssHandler {
    public TransGnssHandler(SatelliteStationService satelliteStationService, ObsErvationService obsErvationService) {
        super(satelliteStationService, obsErvationService);
    }

    @Override
    protected boolean doProcess(GnssFile gnssFile) {

        if (StringUtils.isEmpty(gnssFile.getDirFiles())) {
            return true;
        }
        Path oPath = null;
        List<String> append = new ArrayList<>();
        for (String fileName : gnssFile.getDirFiles()) {
            if (fileName.endsWith("o")) {
                oPath = Paths.get(super.getTempPath("o", gnssFile), fileName);
                if (Files.notExists(oPath)) {
                    continue;
                }
//                Path target = Paths.get(super.getDataFormatPath(GnssFormat.RINEX,gnssFile),fileName.substring(fileName.lastIndexOf(".")+1),fileName);
                // 执行TEQC 完成O文件采样
                ShellRun.execOutPut(super.getTeqcRun(gnssFile.getIndex()) + " -O.dec 30s " + oPath.normalize().toString(), super.getUnZipPath(gnssFile, fileName));
                gnssFile.getClearList().add(oPath.normalize().toString());
                this.checkO(super.getUnZipPath(gnssFile, fileName), gnssFile);
//                SendJmsInfo.sendGnssMq(8, fileName, 0);
//                SendJmsInfo.sendGnssMq(9, fileName, 0);
//                SendJmsInfo.sendGnssMq(10, fileName, 0);
                continue;
            }
            if (fileName.endsWith("dat")) {
                oPath = Paths.get(super.getTempPath("d", gnssFile), fileName);
                if (Files.notExists(oPath)) {
                    continue;
                }
                transDat(oPath, gnssFile,append);
            }
        }
        gnssFile.getDirFiles().addAll(append);
        return true;
    }

    private void transDat(Path dPath, GnssFile gnssFile, List<String> append ) {
        String fileName = dPath.getFileName().toString().toLowerCase();
        String saveName = super.getOrReplaceStation(fileName.substring(0,4).toLowerCase(), gnssFile);
        if (fileName.indexOf("_") != -1) {
            saveName = super.getOrReplaceStation(gnssFile.getStationId().substring(0, 4), gnssFile);
        }
        saveName = saveName + NumberUtils.format(gnssFile.getDayOfYear(), "000") + GnssContext.TIME_CODE[gnssFile.getHoure()] + NumberUtils.format(gnssFile.getMinute(),"00") + "." + gnssFile.getYear().toString().substring(2);
        FileUtils.createDirs(super.getUnZipPath(gnssFile, saveName + "n"));

        String commond = super.getTeqcRun(gnssFile.getIndex()) + " -tr do -week " + gnssFile.getDate().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        commond += " +nav " + super.getUnZipPath(gnssFile, saveName + "n").normalize().toString() + " " + dPath.normalize().toString();

        Path oPath = Paths.get(super.getTempPath("o", gnssFile), saveName + "o");
        ShellRun.execOutPut(commond, oPath);
        // 执行TEQC 完成O文件采样
        ShellRun.execOutPut(super.getTeqcRun(gnssFile.getIndex()) + " -O.dec 30s " + oPath.normalize().toString(), super.getUnZipPath(gnssFile, saveName + "o"));
//        SendJmsInfo.sendGnssMq(8, oPath.getFileName().normalize().toString(), 0);
//        SendJmsInfo.sendGnssMq(9, oPath.getFileName().normalize().toString(), 0);
//        SendJmsInfo.sendGnssMq(10, oPath.getFileName().normalize().toString(), 0);
//        gnssFile.getDirFiles().add(saveName + "o");
        append.add(saveName+"o");
        append.add(saveName+"n");
        gnssFile.getClearList().add(oPath.normalize().toString());
//        SendJmsInfo.sendGnssMq(7, fileName, 0);
    }



    private void checkO(Path oFile, GnssFile gnssFile){
        try {
            // 判断小于30KB的数据
            if (Files.size(oFile)/1024 < 30) {
                this.cleanFiles(oFile,gnssFile);
            }
        }catch (Exception e) {
            this.cleanFiles(oFile,gnssFile);
        }
    }

    private void cleanFiles(Path oFile, GnssFile gnssFile) {
        // 清空临时文件
        gnssFile.getClearList().add(super.getNewStationName(oFile.normalize().toString(),".n"));
        gnssFile.getClearList().add(super.getNewStationName(oFile.normalize().toString(),".n"));
        gnssFile.getClearList().add(super.getNewStationName(oFile.normalize().toString(),".g"));
        gnssFile.getClearList().add(oFile.normalize().toString());
        new CleanGnssHandler(super.getSatelliteStationService(),super.getObsErvationService()).process(gnssFile);

    }

    public static void main(String[] args) {
        try {
            System.out.println(Files.size(Paths.get("D:\\tmp\\GnssServerData\\RT\\GNSSMET\\data\\rinex\\2019\\124\\01\\19o\\jstc124b00.19o"))/1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
